package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Familles;
import net.models.Medicaments;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MedicamentAjout {
	private DataBindingContext	m_bindingContext;

	protected Shell				shellMedicamentAjouter;
	private Text				txtAjouterunPraticien;
	private Text				textNomMed;
	private Text				textCompoMed;
	private Text				textEffetsMed;
	private Text				textContreIndicationMed;
	private Spinner				spinnerPrixEchantillonMed;
	private List<Familles>		familles	= new ArrayList<Familles>();
	private ComboViewer			cmbViewerFamilleMeds;
	private Combo				cmbFamilleMeds;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					MedicamentAjout window = new MedicamentAjout();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		familles = Application.getFamillesMeds();
		Display display = Display.getDefault();
		createContents();
		shellMedicamentAjouter.open();
		shellMedicamentAjouter.layout();
		while (!shellMedicamentAjouter.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shellMedicamentAjouter = new Shell();
		shellMedicamentAjouter.setSize(316, 379);
		shellMedicamentAjouter.setText("SWT Application");
		shellMedicamentAjouter.setLayout(null);

		Composite composite = new Composite(shellMedicamentAjouter, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(204, 102, 0));
		composite.setBounds(0, 0, 300, 42);

		txtAjouterunPraticien = new Text(composite, SWT.NONE);
		txtAjouterunPraticien.setText("Ajouter un médicament");
		txtAjouterunPraticien.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtAjouterunPraticien.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtAjouterunPraticien.setBackground(SWTResourceManager.getColor(204, 102, 0));
		txtAjouterunPraticien.setBounds(10, 10, 211, 21);

		Composite composite_2 = new Composite(shellMedicamentAjouter, SWT.NONE);
		composite_2.setBounds(10, 281, 278, 49);

		Button button = new Button(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shellMedicamentAjouter.close();
			}
		});
		button.setText("Annuler");
		button.setBounds(178, 10, 90, 30);

		Composite composite_1 = new Composite(shellMedicamentAjouter, SWT.NONE);
		composite_1.setBounds(10, 48, 278, 227);

		CLabel label = new CLabel(composite_1, SWT.NONE);
		label.setText("Nom");
		label.setBounds(10, 10, 39, 26);

		CLabel label_1 = new CLabel(composite_1, SWT.NONE);
		label_1.setText("Famille");
		label_1.setBounds(10, 42, 59, 26);

		CLabel label_2 = new CLabel(composite_1, SWT.NONE);
		label_2.setText("Composition");
		label_2.setBounds(10, 74, 91, 26);

		CLabel label_3 = new CLabel(composite_1, SWT.NONE);
		label_3.setText("Effets");
		label_3.setBounds(10, 106, 91, 26);

		CLabel label_4 = new CLabel(composite_1, SWT.NONE);
		label_4.setText("Contre Indications");
		label_4.setBounds(10, 138, 126, 26);

		CLabel label_5 = new CLabel(composite_1, SWT.NONE);
		label_5.setText("Prix Echantillon");
		label_5.setBounds(10, 170, 126, 26);

		textNomMed = new Text(composite_1, SWT.BORDER);
		textNomMed.setBounds(142, 10, 126, 26);

		textCompoMed = new Text(composite_1, SWT.BORDER);
		textCompoMed.setBounds(142, 74, 126, 26);

		textEffetsMed = new Text(composite_1, SWT.BORDER);
		textEffetsMed.setBounds(142, 106, 126, 26);

		textContreIndicationMed = new Text(composite_1, SWT.BORDER);
		textContreIndicationMed.setBounds(142, 138, 126, 26);

		spinnerPrixEchantillonMed = new Spinner(composite_1, SWT.BORDER);
		spinnerPrixEchantillonMed.setBounds(142, 174, 126, 22);

		cmbViewerFamilleMeds = new ComboViewer(composite_1, SWT.NONE);
		cmbFamilleMeds = cmbViewerFamilleMeds.getCombo();
		cmbFamilleMeds.setBounds(142, 42, 126, 23);

		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Session session = HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerFamilleMeds.getSelection());
				Familles familly = (Familles) (sel.iterator().next());
				Medicaments medica = new Medicaments(textNomMed.getText(), familly, textCompoMed.getText(), textEffetsMed.getText(), textContreIndicationMed.getText(), spinnerPrixEchantillonMed.getSelection());
				session.persist(medica);
				trans.commit();
				session.close();
				shellMedicamentAjouter.close();
			}
		});
		button_1.setText("Valider");
		button_1.setBounds(82, 10, 90, 30);
		m_bindingContext = initDataBindings();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Familles.class, "name");
		cmbViewerFamilleMeds.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		cmbViewerFamilleMeds.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Familles.class).observe(familles);
		cmbViewerFamilleMeds.setInput(selfList);
		//
		return bindingContext;
	}
}
